package fr.ordre66.vlcs.client;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.PropertyException;
import javax.xml.bind.Unmarshaller;

import fr.ordre66.vlcs.utils.ConfigBean;

public class Config extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1867032311304912045L;
	private JPanel contentPane;
	private JTextField tfLogin;
	private JTextField tfPassword;
	private String login, password;
	private String pathSettingsFile = "/tmp/settings.xml";

	/**
	 * Launch the application.
	 */
//	public static void main(String[] args) {
//		EventQueue.invokeLater(new Runnable() {
//			public void run() {
//				try {
//					Config frame = new Config();
//					frame.setVisible(true);
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//		});
//	}

	/**
	 * Create the frame.
	 */
	public Config() {
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		setBounds(100, 100, 290, 163);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblLogin = new JLabel("Login :");
		lblLogin.setBounds(40, 43, 70, 15);
		contentPane.add(lblLogin);
		
		JLabel lblPassword = new JLabel("Password :");
		lblPassword.setBounds(40, 69, 90, 15);
		contentPane.add(lblPassword);
		
		tfLogin = new JTextField();
		tfLogin.setBounds(133, 41, 114, 19);
		contentPane.add(tfLogin);
		tfLogin.setColumns(10);
		
		tfPassword = new JTextField();
		tfPassword.setBounds(133, 67, 114, 19);
		contentPane.add(tfPassword);
		tfPassword.setColumns(10);
		
		JButton btnOk = new JButton("Ok");
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				login = tfLogin.getText();
				password = tfPassword.getText();
				setVisible(false);
				
				ConfigBean confBean = new ConfigBean();
				confBean.setLogin(login);
				confBean.setPassword(password);
			    try {
					JAXBContext context = JAXBContext.newInstance(ConfigBean.class);
					Marshaller m = context.createMarshaller();
					m.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );
					m.marshal( confBean, new FileOutputStream( pathSettingsFile ) );
				} catch (PropertyException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (FileNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (JAXBException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

			}
		});
		btnOk.setBounds(40, 98, 81, 25);
		contentPane.add(btnOk);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				tfLogin.setText(login);
				tfPassword.setText(password);
				setVisible(false);
			}
		});
		btnCancel.setBounds(166, 98, 81, 25);
		contentPane.add(btnCancel);
		
		JLabel lblConfig = new JLabel("Config");
		lblConfig.setBounds(111, 0, 70, 15);
		contentPane.add(lblConfig);
		
		try {
			JAXBContext context = JAXBContext.newInstance(ConfigBean.class);
			Unmarshaller um = context.createUnmarshaller();
			ConfigBean confBean = (ConfigBean) um.unmarshal( new File( pathSettingsFile ) );
			login = confBean.getLogin();
			password = confBean.getPassword();
			tfLogin.setText(login);
			tfPassword.setText(password);
		} catch (JAXBException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}		
	}

	public String getLogin() {
		return login;
	}

	public String getPassword() {
		return password;
	}
	
	
}
